package 线程的状态;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-11-18
 * Time: 9:28
 */
public class SleepAndInterrupt {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println( System.currentTimeMillis() +"sleeping...");
            try {
                Thread.sleep(2000);//当当前t线程睡眠2s钟
            } catch (InterruptedException e) {
                System.out.println(System.currentTimeMillis() + "wake up...");
                e.printStackTrace();
            }
        });
        t.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //让其主线程在1秒的时候就打断t线程,让其唤醒,这个时候就会抛出InterruptedException
        t.interrupt();
        Thread.yield();

    }

}
